package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;


/**
 * Test cases for ReseacherActionBean.
 * 
 * @author Jon Lao
 * @author Jared Sunouchi
 * @author Chiao-Fen Zielinski-Liu
 */
public class TestResearcherActionBean {


  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  private String username = "username";
  private String password = "password";
  private String rschForm = "ResearcherForm";
  private String name = "name";
  
  
  /**
   * Tests adding of researcher, happy trail style.
   * 
   * @throws Exception if error occurs.
   */
  @Test
  @SuppressWarnings("deprecation") //TableCell.asText() is httpunit 1.5.3, needed due to Hackystat
  public void testAddResearcherActionBean() throws Exception {
    WebConversation conversation = new WebConversation();
    String url = testHost + "myisern-1-green/index.jsp";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Checking index.jsp retrieval", "MyISERN 1.3 Login", response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    response = conversation.getResponse(loginRequest);
    loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(username, username);
    loginRequest.setParameter(password, password);
    
    response = conversation.getResponse(loginRequest);
    assertEquals("Checking home.jsp retrieval", "MyISERN 1.3", response.getTitle());
    
    url = testHost + "myisern-1-green/add.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking edit.jsp retrieval", "MyISERN 1.3 - Add", response.getTitle());
    
    WebForm researcherForm = response.getFormWithID("AddResearcherForm");
    WebRequest researcherRequest = researcherForm.getRequest();
    response = conversation.getResponse(researcherRequest);
    
    WebForm addResearcherForm = response.getFormWithID(rschForm);
    WebRequest addResearcherRequest = addResearcherForm.getRequest();
    addResearcherRequest.setParameter(name, "");
    response = conversation.getResponse(addResearcherRequest);
    
    addResearcherForm = response.getFormWithID(rschForm);
    addResearcherRequest = addResearcherForm.getRequest(); 
    
    addResearcherRequest.setParameter("name", "Test Name");
    addResearcherRequest.setParameter("organization", "Test Organization");
    addResearcherRequest.setParameter("email", "Test@TestEmail.com");
    addResearcherRequest.setParameter("pictureLink", "http://Testpicturelink.jpg");
    addResearcherRequest.setParameter("bioStatement", "Test Bio-statement");
    
    response = conversation.getResponse(addResearcherRequest);
    
  
    url = testHost + "myisern-1-green/edit.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking edit.jsp retrieval", "MyISERN 1.3 - Edit", response.getTitle());
  
    researcherForm = response.getFormWithID("EditResearcherForm");
    researcherRequest = researcherForm.getRequest();
    response = conversation.getResponse(researcherRequest);
    
    WebForm editResearcherForm = response.getFormWithID(rschForm); 
    WebRequest editResearcherRequest = editResearcherForm.getRequest();
    
    editResearcherRequest.setParameter(name, "Test Name");
    
    response = conversation.getResponse(editResearcherRequest);
   
    researcherForm = response.getFormWithID(rschForm);
    researcherRequest = researcherForm.getRequest("edit"); 
    response = conversation.getResponse(researcherRequest);

  }
  
  /**
   * Tests editing of researcher, happy trail style.
   *
   * @throws Exception If problems occur
   */
  @Test
  @SuppressWarnings("deprecation") //TableCell.asText() is httpunit 1.5.3, needed due to Hackystat
  public void testEditResearcherActionBean() throws Exception {
    WebConversation conversation = new WebConversation();
    String url = testHost + "myisern-1-green/index.jsp";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Checking index.jsp retrieval", "MyISERN 1.3 Login", response.getTitle());
    
    // Login
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    response = conversation.getResponse(loginRequest);
    loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(username, username);
    loginRequest.setParameter(password, password);
    
    response = conversation.getResponse(loginRequest);
    assertEquals("Checking home.jsp retrieval", "MyISERN 1.3", response.getTitle());
    
    // Go to Edit page.
    url = testHost + "myisern-1-green/edit.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking edit.jsp retrieval", "MyISERN 1.3 - Edit", response.getTitle());
    
    // Go to Edit form for Researcher.
    WebForm researcherForm = response.getFormWithID("EditResearcherForm");
    WebRequest researcherRequest = researcherForm.getRequest();
    response = conversation.getResponse(researcherRequest);
    
    WebForm editResearcherForm = response.getFormWithID(rschForm);
    WebRequest editResearcherRequest = editResearcherForm.getRequest();
    
    // Selecting the name of the researcher to test.
    editResearcherRequest.setParameter(name, "Test Name");    
    response = conversation.getResponse(editResearcherRequest);    
    editResearcherForm = response.getFormWithID(rschForm);
    editResearcherRequest = editResearcherForm.getRequest("edit");
    
    // Changing the researcher's profile.
    editResearcherRequest.setParameter("organization", "Test Organization Edited");
    editResearcherRequest.setParameter("email", "Test@TestEmail.com Edited");
    editResearcherRequest.setParameter("pictureLink", "http://Testpicturelink.jpg Edited");
    editResearcherRequest.setParameter("bioStatement", "Test Bio-statement Edited");
    
    // Testing edit.
    response = conversation.getResponse(editResearcherRequest);
    editResearcherForm = response.getFormWithID(rschForm);
    editResearcherRequest = editResearcherForm.getRequest("edit");
    
    // Testing remove.
    editResearcherForm = response.getFormWithID(rschForm);
    editResearcherRequest = editResearcherForm.getRequest("remove");
    response = conversation.getResponse(editResearcherRequest);
  }  
}
